home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / GZIPOutputStream.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  145 lines

  1. /*
  2.  * @(#)GZIPOutputStream.java    1.11 97/01/24
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util.zip;
  24.  
  25. import java.io.OutputStream;
  26. import java.io.IOException;
  27.  
  28. /**
  29.  * This class implements a stream filter for writing compressed data in
  30.  * the GZIP file format.
  31.  * @version     1.11, 01/24/97
  32.  * @author     David Connelly
  33.  *
  34.  */
  35. public
  36. class GZIPOutputStream extends DeflaterOutputStream {
  37.     /**
  38.      * CRC-32 of uncompressed data.
  39.      */
  40.     protected CRC32 crc = new CRC32();
  41.  
  42.     /*
  43.      * GZIP header magic number.
  44.      */
  45.     private final static int GZIP_MAGIC = 0x8b1f;
  46.  
  47.     /**
  48.      * Creates a new output stream with the specified buffer size.
  49.      * @param out the output stream
  50.      * @param size the output buffer size
  51.      * @exception IOException If an I/O error has occurred.
  52.      */
  53.     public GZIPOutputStream(OutputStream out, int size) throws IOException {
  54.     super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
  55.     writeHeader();
  56.     crc.reset();
  57.     }
  58.  
  59.     /**
  60.      * Creates a new output stream with a default buffer size.
  61.      * @param out the output stream
  62.      * @exception IOException If an I/O error has occurred.
  63.      */
  64.     public GZIPOutputStream(OutputStream out) throws IOException {
  65.     this(out, 512);
  66.     }
  67.  
  68.     /**
  69.      * Writes array of bytes to the compressed output stream. This method
  70.      * will block until all the bytes are written.
  71.      * @param buf the data to be written
  72.      * @param off the start offset of the data
  73.      * @param len the length of the data
  74.      * @exception IOException If an I/O error has occurred.
  75.      */
  76.     public synchronized void write(byte[] buf, int off, int len)
  77.     throws IOException
  78.     {
  79.     super.write(buf, off, len);
  80.     crc.update(buf, off, len);
  81.     }
  82.  
  83.     /**
  84.      * Finishes writing compressed data to the output stream without closing
  85.      * the underlying stream. Use this method when applying multiple filters
  86.      * in succession to the same output stream.
  87.      * @exception IOException if an I/O error has occurred
  88.      */
  89.     public void finish() throws IOException {
  90.     if (!def.finished()) {
  91.         def.finish();
  92.         while (!def.finished()) {
  93.         deflate();
  94.         }
  95.         writeTrailer();
  96.     }
  97.     }
  98.  
  99.     /**
  100.      * Writes remaining compressed data to the output stream and closes the
  101.      * underlying stream.
  102.      * @exception IOException if an I/O error has occurred
  103.      */
  104.     public void close() throws IOException {
  105.     finish();
  106.     out.close();
  107.     }
  108.   
  109.     /*
  110.      * Writes GZIP member header.
  111.      */
  112.     private void writeHeader() throws IOException {
  113.     writeShort(GZIP_MAGIC);        // Magic number
  114.     out.write(def.DEFLATED);    // Compression method (CM)
  115.     out.write(0);            // Flags (FLG)
  116.     writeInt(0);            // Modification time (MTIME)
  117.     out.write(0);            // Extra flags (XFL)
  118.     out.write(0);            // Operating system (OS)
  119.     }
  120.  
  121.     /*
  122.      * Writes GZIP member trailer.
  123.      */
  124.     private void writeTrailer() throws IOException {
  125.     writeInt((int)crc.getValue());  // CRC-32 of uncompressed data
  126.     writeInt(def.getTotalIn());    // Number of uncompressed bytes
  127.     }
  128.  
  129.     /*
  130.      * Writes integer in Intel byte order.
  131.      */
  132.     private void writeInt(int i) throws IOException {
  133.     writeShort(i & 0xffff);
  134.     writeShort((i >> 16) & 0xffff);
  135.     }
  136.  
  137.     /*
  138.      * Writes short integer in Intel byte order.
  139.      */
  140.     private void writeShort(int s) throws IOException {
  141.     out.write(s & 0xff);
  142.     out.write((s >> 8) & 0xff);
  143.     }
  144. }
  145.